cmake_minimum_required(VERSION 3.10)

project(pcapHub VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Network)

if(CMAKE_SYSTEM_NAME MATCHES "Windows")
	#Set /utf-8 as default encoding for MSVC compiler
	if(MSVC)
		add_compile_options("/utf-8")
	endif()
	set(PCAP_LIB wpcap)
	if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
		link_directories(${PROJECT_SOURCE_DIR}/npcap/Lib/x64)
	else()
		link_directories(${PROJECT_SOURCE_DIR}/npcap/Lib)
	endif()
	include_directories(${PROJECT_SOURCE_DIR}/npcap/Include)
else()
	set(PCAP_LIB pcap)
endif()

set(PROJECT_SOURCES
	main.cpp
	dlgpcaphub.cpp
	dlgpcaphub.h
	dlgpcaphub.ui
	io/pcapio.h
	io/pcapio.cpp
	io/cap_thread.h
	io/cap_thread.cpp
	io/tcptunnel.h
	io/tcptunnel.cpp
	io/sslserver.h
	io/sslserver.cpp
	tb_interface/tb_interface.h
	tb_interface/cmdlineparser.h
	tb_interface/listen_thread.h
	tb_interface/listen_thread.cpp
	resource.qrc
	pcapHub.rc
	${TS_FILES}
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
	qt_add_executable(pcapHub
		MANUAL_FINALIZATION
		${PROJECT_SOURCES}
	)
	#FIX MINGW64 Qt6 static link problems.
	if (QT_FEATURE_static STREQUAL "ON" AND WIN32 AND MINGW)
		set_property(TARGET harfbuzz::harfbuzz PROPERTY
			IMPORTED_IMPLIB ${harfbuzz_DIR}../../../libharfbuzz.dll.a )
		set(PCAPLinkExtra "pthread" )
	else()
		set(PCAPLinkExtra "" )
	endif()
else()
	add_executable(pcapHub
		${PROJECT_SOURCES}
	)
	set(PCAPLinkExtra "" )
endif()

target_link_libraries(
	pcapHub
	PRIVATE
	Qt${QT_VERSION_MAJOR}::Widgets
	Qt${QT_VERSION_MAJOR}::Network
	${PCAP_LIB}
	${PCAPLinkExtra}
)

set_target_properties(pcapHub PROPERTIES
	MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
	MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
	MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
	MACOSX_BUNDLE TRUE
	WIN32_EXECUTABLE TRUE
)

install(TARGETS pcapHub
	BUNDLE DESTINATION .
	LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

if(QT_VERSION_MAJOR EQUAL 6)
	qt_finalize_executable(pcapHub)
endif()
